package com.grass.cropTradingCenter.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.grass.cropTradingCenter.domain.repository.CropRepository;
import com.grass.cropTradingCenter.domain.repository.GoodsPackageRepository;
import com.grass.cropTradingCenter.mapper.GoodsMapper;
import com.grass.cropTradingCenter.mapper.MiniShopMapper;
import com.grass.cropTradingCenter.pojo.dto.CropDto;
import com.grass.cropTradingCenter.pojo.entity.Goods;
import com.grass.cropTradingCenter.pojo.entity.GoodsPackage;
import com.grass.cropTradingCenter.pojo.entity.MiniShop;
import com.grass.cropTradingCenter.pojo.po.QueryImage;
import com.grass.cropTradingCenter.pojo.vo.user.CropSearchVo;
import com.grass.cropTradingCenter.service.ImageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CropRepositoryImpl implements CropRepository {

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    GoodsPackageRepository goodsPackageRepository;

    @Autowired
    ImageService imageService;

    @Autowired
    MiniShopMapper miniShopMapper;

    @Override
    public List<Goods> getGoodsList(CropSearchVo vo) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Goods::getCheckFlag, 1)
                .eq(Goods::getSellFlag, 1)
                .eq(Goods::getDelFlag, 0);

        if (StringUtils.isNoneBlank(vo.getKey())) {
            queryWrapper.and(wrapper -> {
                wrapper.like(Goods::getGoodsName, vo.getKey())
                        .or().like(Goods::getGoodsDescription, vo.getKey());
            });
        }

        return goodsMapper.selectList(queryWrapper);
    }

    @Override
    public Goods cropDetail(int id, boolean isAddBrowse) {
        Goods goods = goodsMapper.selectById(id);
        if(isAddBrowse){
            goods.setBrowseNum(goods.getBrowseNum() + 1);
            goodsMapper.updateById(goods);
        }
        return goods;
    }

    @Override
    public void updateStarNum(int goodsId, int num) {
        Goods goods = goodsMapper.selectById(goodsId);
        goods.setStartNum(goods.getStartNum() + num);
        goodsMapper.updateById(goods);
    }

    @Override
    public List<Goods> getGoodsListByShopId(int shopId) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Goods::getShopId, shopId)
                .eq(Goods::getCheckFlag, 1)
                .eq(Goods::getSellFlag, 1)
                .eq(Goods::getDelFlag, 0);
        return goodsMapper.selectList(queryWrapper);
    }

    @Override
    public List<CropDto> getCropByShopId(int shopId) {
        List<Goods> goodsList = getGoodsListByShopId(shopId);
        return covertCrop(goodsList);
    }

    private List<CropDto> covertCrop(List<Goods> goodsList) {
        return goodsList.stream().map(goods -> {
            CropDto cropDto = new CropDto();
            BeanUtils.copyProperties(goods, cropDto);
            List<String> imageList = imageService.getImageList(new QueryImage(goods.getGoodsId(), 1));
            cropDto.setDetailImages(imageList);

            List<GoodsPackage> packages = goodsPackageRepository.getGoodsPackageByGoodsId(goods.getGoodsId());

            BigDecimal minPrice = new BigDecimal("9999");
            for (GoodsPackage aPackage : packages) {
                minPrice = minPrice.min(aPackage.getPrice());
            }
            cropDto.setPackages(packages);
            cropDto.setMinPrice(minPrice);

            MiniShop miniShop = miniShopMapper.selectById(goods.getShopId());
            cropDto.setShopName(miniShop.getShopName());
            return cropDto;
        }).collect(Collectors.toList());
    }
}
